package com.cy.test;

import com.cy.mapper.UserMapper2;
import com.cy.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class mybatistest2 {
    InputStream stream = null;
    SqlSessionFactory build = null;
    SqlSession sqlSession = null;
    UserMapper2 mapper = null;

    @Before
    public void testbefore () throws Exception {
        String s = "mybatis-config.xml";
        stream = Resources.getResourceAsStream(s);
        build = new SqlSessionFactoryBuilder().build(stream);
        sqlSession = build.openSession();
        mapper = sqlSession.getMapper(UserMapper2.class);
    }

    @After
    public void testAfter () {
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void test001 () {
        List<User> users = mapper.selectFindAll();
        users.forEach(System.out :: println);

    }

    @Test
    public void test002 () {
        User iu = mapper.getUserById(4);
        System.out.println(iu); //这里的 iu   是 对象吗?  请问  是对象 直接打印 为什么不是地址值
    }

    @Test
    public void test003 () {
//            int i = mapper.insertUser(new User(null, "王也", 20));

        User user = new User(null, "王也", 20);
        int i = mapper.insertUser(user);

        System.out.println(i); //这里的 iu   是 对象吗?  请问  是对象 直接打印 为什么不是地址值
        test001();
    }

    @Test
    public void test004 () {
        int i = mapper.deleteUserById(5);

        System.out.println(i); //这里的 iu   是 对象吗?  请问  是对象 直接打印 为什么不是地址值
        test001();
    }

        @Test
    public void test005 () {
            Map<String, Object> map = new HashMap<>();
            map.put("age", 101);
            map.put("id", 1);

            int i = mapper.updateUser2(map);
            System.out.println(i);
        }

                @Test
    public void test006 () {
                    List<User> userList = mapper.selectList("张");
                    userList.forEach(System.out::println);

        }


}
